package cn.droidlover.xrecyclerview.demo.slice;


import cn.droidlover.xrecyclerview.XRecyclerView;
import cn.droidlover.xrecyclerview.demo.ResourceTable;
import cn.droidlover.xrecyclerview.utils.LogUtils;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class SettingProvider<T> extends XRecyclerView.RecyclerAdapter<T>  {
        // ListContainer的数据集合
        private List<String> settingList;
        private final Context context;

        public SettingProvider(List<T> list, Context context) {
            super(context,list);
            this.settingList = (List<String>) list;
            this.context = context;
        }
    public void addData(List<String> data) {
        this.settingList=data;
    }
        // 用于保存列表项中的子组件信息
        private static class SettingHolder {

            Text settingText;//内容
            Text text_noData;  //footer
            DirectionalLayout head_layout; //header
            SettingHolder(Component component) {

                settingText = (Text) component.findComponentById(ResourceTable.Id_item_index);
                text_noData = (Text) component.findComponentById(ResourceTable.Id_text_noData);

                head_layout=(DirectionalLayout) component.findComponentById(ResourceTable.Id_head_layout);

            }

        }


        @Override
        public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
            final Component cpt;
            SettingHolder holder;
            String setting = settingList.get(position);
            if (component == null) {
                cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_setting, null, false);

                holder = new SettingHolder(cpt);
                // 将获取到的子组件信息绑定到列表项的实例中
                cpt.setTag(holder);

            } else {
                cpt = component;
                // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
                holder = (SettingHolder) cpt.getTag();

            }

            LogUtils.info("position=",position+"");
            if(position==59){
                holder.text_noData.setVisibility(Component.VISIBLE);
            }else{
                holder.text_noData.setVisibility(Component.HIDE);

            }
            holder.settingText.setText(setting);
            if(position==0){
                //显示head布局
                holder.head_layout.setVisibility(Component.VISIBLE);
            }else{
                holder.head_layout.setVisibility(Component.HIDE);

            }
            return cpt;
        }
    }

